package cn.nb12.xbox.quartz;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.nb12.xbox.Param;
import cn.nb12.xbox.po.Area;
import cn.nb12.xbox.po.Games;
import cn.nb12.xbox.po.Queryname;
import cn.nb12.xbox.service.AreaService;
import cn.nb12.xbox.service.GamesService;
import cn.nb12.xbox.service.PriceService;
import cn.nb12.xbox.service.QuerynameService;
import cn.nb12.xbox.utils.Assist;
import cn.nb12.xbox.utils.DateUtil;
import cn.nb12.xbox.utils.ExchangeRateUtil;
import cn.nb12.xbox.utils.FindGameUtil;
import cn.nb12.xbox.utils.GenerateBusinessCodeService;
import cn.nb12.xbox.utils.NumUtil;
import cn.nb12.xbox.utils.UpdateAllGame;

/**
 * 定时更新费率
 * @author yh
 *
 */
public class DefaultJob {
	
	@Autowired
	private AreaService areaService;
	
	@Autowired
	private UpdateAllGame updateAllGame;
	private Logger log=LoggerFactory.getLogger(DefaultJob.class);
	
	@Autowired
	private QuerynameService querynameService;
	
	@Autowired
	private GamesService gamesService;
	
	@Autowired
	private GenerateBusinessCodeService generateBusinessCodeService;
	
	@Autowired
	private PriceService priceService;
	
	/**
	 * 更新费率
	 */
	public void updateRate() {
		log.info("更新费率开始");
		Assist assist=new Assist();
		assist.setRequires(new Assist().new WhereRequire<String>(" and updatetime is null ", null));
		assist.setRequires(Assist.orLt("updatetime", DateUtil.getNowDate1()));
		List<Area> area = areaService.selectArea(assist);
		if(area!=null && area.size()>0) {//更新费率
			for (Area area2 : area) {
				String rate = ExchangeRateUtil.getExchangeRate(area2.getCurrency(), Param.CHINA_CURRENCY);
				area2.setRate(NumUtil.getDouble(rate, 0.0));
				areaService.updateNonEmptyAreaById(area2);
			}
		}
		log.info("更新费率结束");
	}
	
	/**
	 * 更新所有游戏
	 */
	public void updateAll() {
		log.info("更新所有游戏开始");
		this.deleteRepeat();
		updateAllGame.updateAllGame();
		log.info("更新所有游戏结束");
	}
	
	/**
	 * 更新初始化的游戏
	 */
	public void updateInit() {
		
		log.info("初始化游戏数据查询");
		
		this.deleteRepeat();
		
		// 查询所有国家,并查询游戏入库
		List<Area> areas = areaService.selectArea(null);
		if (areas != null && areas.size() > 0) {
			List<Thread> ts = new ArrayList<>();
			for (Area area : areas) {
				ts.add(new Thread() {
					@Override
					public void run() {
						Vector<Games> result = new Vector<>();
						List<Queryname> list = querynameService.selectQueryname(null);
						querynameService.deleteQueryname(null);//删除所有
						List<String> games=new ArrayList<>();
						if(list!=null && list.size()>0) {
							for (Queryname queryname : list) {
								List<String> games2 = FindGameUtil.getGames(Param.CLIENT_ID, queryname.getName(),
										area.getCode());
								games.addAll(games2);
							}
							
						}
						if (games != null && games.size() > 0) {
							for (String code : games) {
								Assist assist = new Assist();
								assist.setRequires(Assist.andEq("code", code));
								List<Games> selectGames = gamesService.selectGames(assist);
								if (selectGames == null || selectGames.size() < 1) {
									Games games2 = new Games();
									games2.setCode(code);
									games2.setId(generateBusinessCodeService.doWork());
									result.add(games2);
									gamesService.insertNonEmptyGames(games2);
								}
							}
						}
					}
				});
			}
			for (Thread thread : ts) {
				thread.start();
			}
			for (Thread thread : ts) {
				try {
					thread.join();
				} catch (InterruptedException e) {
					log.error("多线程出现问题", e);
				}
			}
		}
		
		log.info("更新初始化游戏开始");
		updateAllGame.updateInitData();
		log.info("更新初始化游戏结束");
	}
	
	/**
	 * 删除重复数据
	 */
	private void deleteRepeat() {
		log.info("删除重复数据开始");
		try {
			priceService.deleteRepeat();
		} catch (Exception e) {
			log.error("删除重复价格",e);
		}
		try {
			gamesService.deleteRepeat();
		} catch (Exception e) {
			log.error("删除重复游戏数据",e);
		}
		
		log.info("删除重复数据结束");
	}
}
